<template>
  <div class="list-container">
    <div class="search-wrap clearfix">
      <div class="search-wrap-conditions fl">
        <el-input
          v-model="listQuery.condition.key"
          class="input-width"
          placeholder="关键词, 说明"
          @keyup.enter.native="resetPage"
        />
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <el-select
          v-model="listQuery.condition.targetType"
          clearable
          placeholder="请选择类型"
          @change="resetPage"
        >
          <el-option
            v-for="item in conditionType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>

      <div class="search-wrap-conditions fl marginL10">
        <div class="block">
          <el-date-picker
            v-model="timer"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="resetPage"
          />
        </div>
      </div>
      <div class="search-wrap-btn fr">
        <el-button :disabled="loading" type="primary" @click="resetPage">搜索</el-button>
        <el-button :disabled="loading" type="success" @click="add">添加</el-button>
      </div>
    </div>

    <div class="main-wrapper">
      <custom-table :data="listData" :columns="columns">
        <template slot-scope="{ row }">
          <el-button type="text" @click="getDetail(row)">查看对象</el-button>
          <el-button
            :disabled="row.status === 'ignore' ? true : false"
            type="text"
            @click="getProcess(row)"
          >无效举报</el-button>
        </template>
      </custom-table>
      <pagination :total="total" @pagination="pagination" />
    </div>
  </div>
</template>

<script>
import CustomTable from '@/components/CustomTable/index';
import Pagination from '@/components/Pagination/index';
import { getTipOffList, process } from '@/api/report';
import { conditionType } from './data';

export default {
  name: 'Report',
  components: {
    CustomTable,
    Pagination
  },
  data () {
    return {
      loading: false,
      conditionType: conditionType,
      listQuery: {
        condition: {
          endTime: null,
          key: null,
          startTime: null,
          type: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      },

      timer: [],
      listData: [],
      total: 0,
      columns: [
        { label: '序号', value: 'index', width: '80px', align: 'center' },
        { label: '说明', value: 'content', width: '200px' },
        { label: '', value: 'con', width: '20px' },
        { label: '举报对象', value: 'targetTypeTxt', width: '200px', align: 'center' },
        { label: '举报人', value: 'userName', align: 'center' },
        { label: '时间', value: 'createTime', width: '80px', align: 'center' },
        { label: '状态', value: 'statusTxt', align: 'center' },
        { label: '操作', value: 'todo', align: 'center', width: '200px' }
      ]
    };
  },
  created () {
    this.init();
  },
  methods: {
    resetPage () {
      this.listQuery.pager.page = 1;
      this.listQuery.pager.pageSize = 10;
      this.init();
    },
    init () {
      this.loading = true;
      this.listQuery.condition.startTime = this.timer && this.timer.length > 0 ? this.timer[0] : null;
      this.listQuery.condition.endTime = this.timer && this.timer.length > 0 ? this.timer[1] : null;
      this.listQuery.condition.key = this.listQuery.condition.key ? this.listQuery.condition.key : null;
      getTipOffList(this.listQuery)
        .then(res => {
          if (this.checkArr(res.data.records)) {
            res.data.records.map(item => {
              switch (item.status) {
                case 'unprocessed':
                  item.statusTxt = '未处理';
                  break;
                case 'ignore':
                  item.statusTxt = '无效举报';
                  break;
                case 'warn':
                  item.statusTxt = '警告';
                  break;
                case 'disable_content':
                  item.statusTxt = '封禁内容';
                  break;
                case 'disable_account':
                  item.statusTxt = '封号';
                  break;
                case 'enable':
                  item.statusTxt = '恢复正常';
                  break;
              }
              switch (item.targetType) {
                case 'live':
                  item.targetTypeTxt = '【在线课程】' + item.targetName;
                  break;
                case 'teaching_research':
                  item.targetTypeTxt = '【教研】' + item.targetName;
                  break;
                case 'pre_lesson':
                  item.targetTypeTxt = '【备课】' + item.targetName;
                  break;
              }
            });
          }
          this.listData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },
    resetSearch () {
      this.listQuery = {
        condition: {
          keyword: null
        },

        pager: {
          page: 1,
          pageSize: 10
        }
      };
      this.timer = [];
      this.init();
    },
    add () { },
    pagination ({ page, limit }) {
      this.listQuery.pager.page = page;
      this.listQuery.pager.pageSize = limit;
      this.init();
    },
    getProcess (rows) {
      process({
        id: rows.id,
        status: 'ignore'
      }).then(res => {
        rows.status = 'ignore';
        rows.statusTxt = '无效举报';
        this.$message({
          message: '操作成功',
          type: 'success'
        });
      });
    },
    getDetail (rows) {
      console.log(rows);
      switch (rows.targetType) {
        case 'live':// 在线课程
          this.$router.push({
            path: '/activity/activityDetail',
            query: { report: true, param: rows.targetId }
          });
          break;
        case 'teaching_research': // 教研
          this.$router.push({
            path: '/activity/teachingDetail',
            query: { report: true, status: rows.status, param: rows.targetId }
          });
          break;
        case 'pre_lesson': // 备课
          this.$router.push({
            path: `/content/teachingDetail`,
            query: { report: true, param: rows.targetId }
          });
          break;
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
